package com.fc.advbox.controller.open;

import com.fc.advbox.common.ResponseJson;
import com.fc.advbox.dto.open.PublicTokenRespDto;
import com.fc.advbox.dto.user.UserPointDto;
import com.fc.advbox.entity.AdvUserEntity;
import com.fc.advbox.entity.PublicTokenEntity;
import com.fc.advbox.entity.UserPointEntity;
import com.fc.advbox.service.IPublicTokenService;
import com.fc.advbox.service.ISmsHistoryService;
import com.fc.advbox.service.IUserPointService;
import com.fc.advbox.service.IUserService;
import com.fc.advbox.utils.DateTimeHelper;
import com.fc.advbox.utils.RRException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * Description:
 * User: Charles.Gao
 * Date: 2018-07-19 16:16
 */
@RestController
@RequestMapping("/api/open")
@Api(value="开放接口- 用于第三方应用调用相关的API", tags={"开放接口"})
public class PublicController {

    private final IUserPointService userPointService;
    private final IPublicTokenService publicTokenService;
    private final IUserService userService;

    @Autowired
    public PublicController(IUserPointService userPointService, IPublicTokenService publicTokenService, IUserService userService) {
        this.userPointService = userPointService;
        this.publicTokenService = publicTokenService;
        this.userService = userService;
    }

    @ApiOperation(value = "POST -- 增加积分",
            notes = " REST 方式请求" +
                    "Request Header: <ul>" +
                    "<li><i>必填参数: appid: 客户端ID</i></li>" +
                    "<li><i>必填参数: secret: 应用秘钥</i></li>" +
                    "<li><i>可选参数: access_token: 令牌</i></li>" +
                    "</ul>" +
                    "Request Body: <ul>" +
                    "<li><i>必填参数: userId: 用户ID</i></li>" +
                    "<li><i>必填参数: point: 获取积分值，整数型，不能为空，可以为负整数和0</i></li>" +
                    "<li><i>可选参数: descn: 积分描述</i></li>" +
                    "</ul>")
    @PostMapping("/point")
    public ResponseJson doAddUserPoint(@RequestBody UserPointDto userPoint) {

        if(userPoint.getUserId() == null) {
            return ResponseJson.error(ResponseJson.BAD_REQUEST, "UserId不能为空");
        }
        if(userPoint.getPoint() == null) {
            return ResponseJson.error(ResponseJson.BAD_REQUEST, "积分不能为空");
        }

        UserPointEntity entityBefore = userPointService.queryByUserId(userPoint.getUserId());
        if(entityBefore.getPoint()< Math.abs(userPoint.getPoint())){
            return ResponseJson.error(ResponseJson.BAD_REQUEST, "积分不足");
        }

        UserPointEntity entity = new UserPointEntity();
        entity.setPoint(userPoint.getPoint());
        entity.setUserId(userPoint.getUserId());
        userPointService.addPoint(entity,userPoint.getDescn());
        return ResponseJson.success();
    }

    @ApiOperation(value = "GET -- 根据手机号获取用户积分",
            notes = " REST 方式请求" +
                    "Request Header: <ul>" +
                    "<li><i>必填参数: appid: 客户端ID</i></li>" +
                    "<li><i>必填参数: secret: 应用秘钥</i></li>" +
                    "<li><i>可选参数: access_token: 令牌</i></li>" +
                    "</ul>" +
                    "Request Parameter: <ul>" +
                    "<li><i>必填参数: phone: 手机号</i></li>" +
                    "</ul>")
    @GetMapping("/point")
    public ResponseJson doGetUserPointByPhone(@RequestParam String phone) {

        if(phone == null || "".equals(phone)) {
            return ResponseJson.error(ResponseJson.BAD_REQUEST,"手机号不能为空");
        }

        AdvUserEntity user = userService.queryByMobile(phone);
        if(user == null){
            return ResponseJson.error(ResponseJson.NOT_FOUND, "该手机号的用户不存在");
        }
        UserPointEntity entity = userPointService.queryObject(user.getId());
        return ResponseJson.success(entity);
    }

}
